<template>
  <div id="components-modal-demo-position">
    <a-modal
      centered
      :visible="true"
      :footer="null"
      @cancel="cancel"
      class="addForm"
    >
      <a-form-model
        ref="ruleForm"
        :model="params"
        :rules="rules"
        class="joinForm"
        v-bind="layout"
      >
        <a-form-model-item ref="name" label="1.您的姓名" prop="name">
          <a-input
            placeholder="请输入姓名"
            v-model="params.name"
            @blur="
              () => {
                $refs.name.onFieldBlur()
              }
            "
          />
        </a-form-model-item>
        <a-form-model-item
          ref="company"
          label="2.您所在单位名称"
          prop="company"
        >
          <a-input
            placeholder="请输入单位名称"
            v-model="params.company"
            @blur="
              () => {
                $refs.company.onFieldBlur()
              }
            "
          />
        </a-form-model-item>
        <a-form-model-item ref="job" label="3.您的职务" prop="job">
          <a-input
            placeholder="请输入职务姓名"
            v-model="params.job"
            @blur="
              () => {
                $refs.job.onFieldBlur()
              }
            "
          />
        </a-form-model-item>
        <a-form-model-item ref="phone" label="4.您的联系电话" prop="phone">
          <a-input
            placeholder="请输入联系电话"
            v-model="params.phone"
            @blur="
              () => {
                $refs.phone.onFieldBlur()
              }
            "
          />
        </a-form-model-item>
        <a-form-model-item
          ref="isNav"
          label="5.您的单位近三年是否开展过专利导航"
          prop="isNav"
        >
          <a-radio-group
            v-model="params.isNav"
            @blur="
              () => {
                $refs.isNav.onFieldBlur()
              }
            "
          >
            <a-radio :value="0"> 是 </a-radio>
            <a-radio :value="1"> 否 </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item
          ref="plan"
          label="6.您的单位目前是否开展专利导航的需求或计划？针对什么技术？"
          prop="plan"
        >
          <a-textarea
            placeholder="请输入专利导航的需求或计划"
            v-model="params.plan"
            @blur="
              () => {
                $refs.plan.onFieldBlur()
              }
            "
            :rows="4"
          />
        </a-form-model-item>
      </a-form-model>
      <div class="btnClass">
        <a-button type="primary" class="commonBtn" @click="confirmClick"
          >提交</a-button
        >
      </div>
    </a-modal>
  </div>
</template>
<script>
import { applyNav } from '@/api/dataNavigation'
var reg = new RegExp(
  '^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$'
) // 校验手机号
export default {
  props: {},
  data() {
    return {
      Visible: true,
      params: {
        name: '',
        company: '',
        job: '',
        phone: '',
        isNav: '',
        plan: '',
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名!', trigger: 'blur' }
        ],
        company: [
          { required: true, message: '请输入单位名称!', trigger: 'blur' }
        ],
        job: [
          { required: true, message: '请输入职务姓名!', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入联系电话!', trigger: 'blur' },
          {
            validator: (rule, val, callback) => {
              if (!reg.test(val)) {
                callback(new Error('请输入正确的手机号码!'))
              } else {
                callback()
              }
            }
          }
        ],
        isNav: [
          { required: true, message: '请输入近三年是否开展过专利导航!', trigger: 'blur' }
        ],
        plan: [
          { required: true, message: '请输入专利导航的需求或计划!', trigger: 'blur' }
        ],
      },
      layout: {
        labelCol: { span: 24 },
        wrapperCol: { span: 24 }
      }
    }
  },
  methods: {
    handleCancel() {
      this.$emit('cancelVisible', false)
    },
    cancel() {
      this.$emit('cancel')
    },
    confirmClick() {
      let isBreak = false
      for (let key in this.params) {
        if (this.params[key] === '' && !isBreak) {
          isBreak = true
          this.$message.warning('请完善信息!')
          return
        }
      }
      applyNav(this.params).then(res => {
        if (res.code === 200) {
          this.$message.success('填写成功!')
          this.$emit('addSuccess')
          setTimeout(() => {  //等待弹框提示填写成功，再刷新获取该用户已填写过则不需要在填写此表单
            window.location.reload()
          }, 1000);
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
::v-deep .ant-modal-content {
  width: 530px !important;
}
</style>
